const Goods = require("../../models/goods");
const { Op } = require("sequelize");

//新增一条数据
exports.addGoods = async function(goods){
    return Goods.create({
        ...goods
    })
}

//获取一条数据
exports.getGoods = async function(id){
    return Goods.findByPk(id,{
        attributes:{exclude:['createdAt','updatedAt','deletedAt']}
    });
}

//更新一条数据
exports.updateGoods = async function(id,goods){
    delete goods.id;
    return Goods.update(
        {...goods},
        {where: {id: id}, individualHooks: true}
    )
}

//删除一条数据
exports.deleteGoods = async function(id){
    return Goods.destroy({
        where:{id},
        individualHooks:true
    })
}


//获取活动列表
exports.getGoodsList = async function(keyword,page,size){
    page = Number(page);
    size = Number(size);
    const {rows,count} = await Goods.findAndCountAll({
        where:{
            title:{
                [Op.like]: `%${keyword}%`
            }
        },
        attributes:{exclude:['createdAt','updatedAt','deletedAt']},
        limit:size,
        offset:(page-1) * size,
        order: [['num', 'DESC']],
    });
    return {
        rows,count
    }
}